package com.cqie.secondhandtransaction.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.cqie.secondhandtransaction.DTO.DataList;
import com.cqie.secondhandtransaction.DTO.UserDetailsDTO;
import com.cqie.secondhandtransaction.aspect.AvoidRepeatSubmit;
import com.cqie.secondhandtransaction.entity.CollectShopInfo;
import com.cqie.secondhandtransaction.entity.ProductInfo;
import com.cqie.secondhandtransaction.entity.UserInfo;
import com.cqie.secondhandtransaction.enums.StatusEnum;
import com.cqie.secondhandtransaction.exception.BusinessException;
import com.cqie.secondhandtransaction.mapper.CollectShopInfoMapper;
import com.cqie.secondhandtransaction.mapper.ProductInfoMapper;
import com.cqie.secondhandtransaction.mapper.UserInfoMapper;
import com.cqie.secondhandtransaction.service.CollectShopInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cqie.secondhandtransaction.utils.WebSecurityUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * 收藏的商品 服务实现类
 * </p>
 *
 * @author gdy
 * @since 2025-02-10
 */
@Service
public class CollectShopInfoServiceImpl extends ServiceImpl<CollectShopInfoMapper, CollectShopInfo> implements CollectShopInfoService {

    @Autowired
    private CollectShopInfoMapper collectShopInfoMapper;
    @Autowired
    private ProductInfoMapper productInfoMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    /**
     * 收藏夹列表
     *
     * @return
     */
    @Override
    public DataList<ProductInfo> getCollectShopList() {
        UserDetailsDTO user = WebSecurityUtil.getCurrentUser();
        List<CollectShopInfo> collectShopInfos = collectShopInfoMapper.selectList(new LambdaQueryWrapper<CollectShopInfo>().eq(CollectShopInfo::getUserId, user.getId()));
        collectShopInfos = Objects.nonNull(collectShopInfos) ? collectShopInfos : new ArrayList<>();
        List<String> statusList = Arrays.asList(StatusEnum.S_PUB.getCode(), StatusEnum.SOLD_OUT.getCode());
        DataList<ProductInfo> dataList = new DataList<>();
        Optional.ofNullable(productInfoMapper.selectList(
                new LambdaQueryWrapper<ProductInfo>()
                        .in(ProductInfo::getId, collectShopInfos.stream().map(CollectShopInfo::getProductId).collect(Collectors.toList()))
                        .in(ProductInfo::getStatus, statusList))).ifPresent(list -> {
                            list.forEach(productInfo -> {
                                UserInfo userInfo = Optional.ofNullable(userInfoMapper.selectById(productInfo.getSallorBy())).orElse(new UserInfo());
                                productInfo.setSellerAvatar(userInfo.getPicture());
                            });
                            dataList.setDataList(list);
        });
        return dataList;
    }

    /**
     * 加入/移除收藏夹
     *
     * @param id
     */
    @AvoidRepeatSubmit
    @Override
    public void addFavorites(Integer id) {
        UserDetailsDTO user = WebSecurityUtil.getCurrentUser();
        //查询商品
        ProductInfo productInfo = productInfoMapper.selectById(id);
        if (Objects.isNull(productInfo)) {
            throw new BusinessException("商品不存在！");
        } else if (Objects.equals(productInfo.getSallorBy(), user.getId())) {
            throw new BusinessException("不能收藏自己发布的商品");
        } else if (Objects.equals(productInfo.getStatus(), StatusEnum.SOLD_OUT.getCode())) {
            throw new BusinessException("商品已被交易！");
        }
        CollectShopInfo collectShopInfo = collectShopInfoMapper.selectOne(new LambdaQueryWrapper<CollectShopInfo>().eq(CollectShopInfo::getProductId, id).eq(CollectShopInfo::getUserId, user.getId()));
        if(Objects.nonNull(collectShopInfo)) {
            collectShopInfoMapper.deleteById(collectShopInfo.getId());
            return;
        }
        CollectShopInfo info = new CollectShopInfo();
        info.setProductId(id);
        info.setUserId(user.getId());
        collectShopInfoMapper.insert(info);
    }

}
